/**
 * Copyright (c) 2007,中企动力华南研发中心<br>
 * All rights reserved.<br>
 */
package com.ce.pms.custservice.web;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.custservice.service.ICheckService;
import com.ce.pms.custservice.service.IEventManageService;
import com.ce.pms.custservice.vo.CheckRecordVO;
import com.ce.pms.custservice.vo.CustServiceEventVO;
import com.ce.pms.custservice.vo.QuestionAndAnswerVO;
import com.ce.pms.custservice.vo.ReturnVisitRecordVO;
import com.ce.pms.system.custservice.service.IReturnVisitItemService;
import com.ce.pms.system.custservice.service.IReturnVisitWayService;
import com.ce.pms.system.custservice.vo.ReturnVisitItemVO;
import com.ce.pms.system.custservice.vo.ReturnVisitWayVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.annotation.Out;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述：<br>
 * @author 易贤建 创建日期：2008-12-18<br>
 */
public class CheckRecordAction extends PmsBaseAction {
	/**
	 * 回访登记Service
	 */
	private ICheckService checkService = null;
	/**
	 * 回访方式
	 */
	private IReturnVisitWayService returnVisitWayService = null;
	/**
	 * 回访项目
	 */
	private IReturnVisitItemService returnVisitItemService = null;
	/**
	 * 客户事件Service
	 */
	private IEventManageService eventManageService;
	/**
	 * 功能描述:初始化到主页面<br>
	 * @param customerServiceEventID
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-18  <br>
	 */
	public String initMain(@Read(key="customerServiceEventID")
								@Out(key="customerServiceEventID")Long customerServiceEventID){
		return "main";
	}
	/**
	 * 功能描述:查询回访管理<br>
	 * @param returnVisitRecotd
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-19  <br>
	 */
	public String queryReturnVisitRecordByPage(@Read CheckRecordVO checkRecordVO,
											  @Read(key = "ec_crd",defaultValue="10")	int pageSize,
								             @Read(key = "ec_p", defaultValue="1")	int pageNo){
		checkRecordVO.setPageNo(pageNo);
		checkRecordVO.setPageSize(pageSize);
		Page page = checkService.queryCheckRecordByPage(checkRecordVO);
		setAttribute("checkRecordList", page.getResult());
		setAttribute("totalRows",Integer.valueOf(String.valueOf(page.getTotalCount())));
		setAttribute("customerServiceEventID", checkRecordVO.getCustomerServiceEventID());
		return "list";
	}
	/**
	 * 功能描述:添加回访登记页面<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-19  <br>
	 */
	public String nvigateAddReturnVisit(@Read(key="customerServiceEventID")Long customerServiceEventID){
		initReturnVisitWay(); //缓存回访方式
		initReturnVisitItem(); //缓存回访项目
		CustServiceEventVO custServiceEventVO = eventManageService.getCustServEventByID(customerServiceEventID);
		setAttribute("custServiceEventVO", custServiceEventVO);
		return "add";
	}
	/**
	 * 功能描述:导航到作废页面<br>
	 * @param keyword
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-22  <br>
	 */
	public String nvigateAside(@Read(key="checkRecordID") @Out(key="keyword") Long keyword,
								@Read(key="custEventId") @Out(key="custEventId")Long eventId){
		return "aside";
	}
	/**
	 * 功能描述:显示详细页面<br>
	 * @param keyword
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-29  <br>
	 */
	public String nvigateDetail(@Read(key="checkRecordID")Long keyword){
		CheckRecordVO vo = checkService.getCheckRecordByID(keyword);
		setAttribute("checkRecord", vo);

		Long visitWayKeyword = vo.getReturnVisitWayID();
		if(visitWayKeyword != null){
			ReturnVisitWayVO returnVisitWayVO = returnVisitWayService.findReturnVisitWayVOById(visitWayKeyword);
			setAttribute("returnVisitWaryVO", returnVisitWayVO);
		}
		initReturnVisitItem(); //缓存回访项目
		return "detail";
	}

	/**
	 * 功能描述:添加一个回访登记<br>
	 * @param returnVisitRecotd
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-19  <br>
	 */
	public String addQuestionair(@Read CheckRecordVO checkRecordVO){
		Set<QuestionAndAnswerVO> answerSet = processAnswer();
		checkRecordVO.setCreator(super.getUserId());
		checkRecordVO.setQuestionAndAnswerSet(answerSet);
		checkService.addQuestionair(checkRecordVO.getCustomerServiceEventID(), checkRecordVO);
		return "resultPage";
	}
	/**
	 * 功能描述:作废抽检登记<br>
	 * @param returnVisitRecotd
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-22  <br>
	 */
	public String setAside(@Read ReturnVisitRecordVO returnVisitRecotd ){
		checkService.setAside(returnVisitRecotd);
		return "resultPage";
	}
	/**
	 * 功能描述:验证是否语序<br>
	 * @param csEventID
	 * Create author:yixianjian <br>
	 * Create on:2008-12-26  <br>
	 */
	public void isAllowCheck(@Read(key="csEventID")Long csEventID){
		eventManageService.isAllowCheck(csEventID);
	}
	/**
	 * 功能描述:处理提交得答案<br>
	 * 页面有多个回访内容，要求对每个回访内容处理。所以对页面得输入框得name进行了一些处理。
	 * 回访ID:   没有任何要求,回访答案：回访ID + “_answer”; 回访备注: 回访ID + “_remark”
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-19  <br>
	 */
	private Set<QuestionAndAnswerVO> processAnswer(){
		String[] returnVisitItems = getRequest().getParameterValues("returnVisitItemID");
		if(returnVisitItems == null) return null;

		Set<QuestionAndAnswerVO> answerSet = new HashSet<QuestionAndAnswerVO>();
		QuestionAndAnswerVO answerTemp = null;
		for(String item : returnVisitItems){
			String answer = getRequest().getParameter(item + "_answer");
			if(BlankUtil.isBlank(answer)) continue;

			answerTemp = new QuestionAndAnswerVO();
			answerTemp.setReturnVisitItemID(Long.valueOf(item)); //回访项目
			answerTemp.setAnswer(answer);

			String remark = getRequest().getParameter(item + "_remark");
			answerTemp.setRemark(remark);
			answerSet.add(answerTemp);
		}
		return answerSet;
	}
	/**
	 * 功能描述:缓存回访方式<br>
	 * Create author:yixianjian <br>
	 * Create on:2008-12-19  <br>
	 */
	private void initReturnVisitWay(){
		List<ReturnVisitWayVO> returnVisitWayList = returnVisitWayService.getReturnVisitWayList();
		setAttribute("returnVisitWayList", returnVisitWayList);
	}
	/**
	 * 功能描述:初始化回访内容列表<br>
	 * Create author:yixianjian <br>
	 * Create on:2008-12-19  <br>
	 */
	private void initReturnVisitItem(){
		List<ReturnVisitItemVO> returnVisitItems = returnVisitItemService.queryAllReturnVisitItemWithValidity();
		setAttribute("returnVisitItemList", returnVisitItems);
	}
	public void setReturnVisitWayService(
			IReturnVisitWayService returnVisitWayService) {
		this.returnVisitWayService = returnVisitWayService;
	}
	public void setReturnVisitItemService(
			IReturnVisitItemService returnVisitItemService) {
		this.returnVisitItemService = returnVisitItemService;
	}
	public void setEventManageService(IEventManageService eventManageService) {
		this.eventManageService = eventManageService;
	}
	public void setCheckService(ICheckService checkService) {
		this.checkService = checkService;
	}
}
